<template>
  <el-dialog :title="!dataForm.id ? '新建' :  isDetail ? '详情':'编辑'" :close-on-click-modal="false" :visible.sync="visible"
    class="cu-dialog cu-dialog_center" lock-scroll width="700px">
    <el-row :gutter="15" class="">
      <el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right"
        :disabled="!!isDetail" :rules="rules">
        <el-col :span="12" v-if="custom.hasFormP('teacherName')">
          <el-form-item label="真实姓名" prop="teacherName">
            <el-input v-model="dataForm.teacherName" placeholder="请输入" clearable :style='{"width":"100%"}'>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="custom.hasFormP('gender')">
          <el-form-item label="性别" prop="gender">
            <el-select v-model="dataForm.gender" placeholder="请选择" clearable :style='{"width":"100%"}'>
              <el-option v-for="(item, index) in genderOptions" :key="index" :label="item.fullName"
                :value="item.enCode">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="custom.hasFormP('mobile')">
          <el-form-item label="联系方式" prop="mobile">
            <el-input v-model="dataForm.mobile" placeholder="请输入" clearable :style='{"width":"100%"}'>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="custom.hasFormP('idCard')">
          <el-form-item label="身份证号码" prop="idCard">
            <el-input v-model="dataForm.idCard" placeholder="请输入" clearable :style='{"width":"100%"}'>
            </el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data () {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        trainId: undefined,
        campusId: undefined,
        teacherId: [],
        teacherName: undefined,
        gender: undefined,
        mobile: undefined,
        idCard: undefined,
      },
      rules: {
        trainId: [
          {
            required: true,
            message: '请输入关联师训',
            trigger: 'change'
          },
        ],
        campusId: [
          {
            required: true,
            message: '请输入报名校区',
            trigger: 'change'
          },
        ],
        teacherId: [
          {
            required: true,
            message: '请输入报名老师',
            trigger: 'change'
          },
        ],
      },
      genderOptions: [],
    }
  },
  computed: {},
  watch: {},
  created () {
    this.$store.dispatch('base/getDictionaryData', { sort: 'sex' }).then(res => {
      this.genderOptions = res
    })
  },
  mounted () {
  },
  methods: {
    goBack () {
      this.$emit('refresh')
    },
    init (id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields();
        if (this.dataForm.id) {
          request({
            url: '/api/campus/train/sign/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data;
          })
        }
      })
    },
    dataFormSubmit () {
      this.$refs['elForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.id) {
            request({
              url: `/api/campus/train/sign`,
              method: 'post',
              data: this.dataForm,
            }).then((res) => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false,
                    this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: '/api/campus/train/sign/' + this.dataForm.id,
              method: 'PUT',
              data: this.dataForm
            }).then((res) => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    },
  }
}
</script>
